﻿using ObjectBakery;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ObjectBakery.Helpers;
using ObjectBakery.Tests.TestDoubles;
using System;
namespace ObjectBakery.Tests
{
    
    
    /// <summary>
    ///This is a test class for ScriptRunnerTest and is intended
    ///to contain all ScriptRunnerTest Unit Tests
    ///</summary>
    [TestClass()]
    [DeploymentItem(@".\Asset.cs")]
    [DeploymentItem(@".\Product.cs")]
    [DeploymentItem(@".\TestScripts\Script.cs")]
    public class ScriptRunnerTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion



        /// <summary>
        ///A test for Execute
        ///</summary>
        [TestMethod()]
        public void InvokeTest()
        {
            ScriptRunner target = new ScriptRunner(Language.CSharp);

            object[] parameters = new object[] { 10, 10 };
            object expected = 100; 
            object actual;
            string MethodName = "Multiply"; //inner one..
            string fullTypeName = "ObjectBakery.Tests.TestScripts.Script"; //The Type is defined in the file below.
            string filePath = @".\TestScripts\Script.cs"; 

            target.Load(fullTypeName, filePath);
            actual = target.Invoke(MethodName, parameters); //Execute should to the invoke.. not Load.

            Assert.AreEqual(expected, actual);
        }


        /// <summary>
        ///A test for Execute
        ///</summary>
        [TestMethod()]

        public void CanBuildInstance()
        {
            ScriptRunner target = new ScriptRunner(Language.CSharp); // TODO: Initialize to an appropriate value

            string fullTypeName = "ObjectBakery.Tests.TestDoubles.Asset";
            string filePath = @".\Asset.cs";

            object asset = target.Build(fullTypeName, filePath);

            Assert.IsNotNull(asset);
        }


        /// <summary>
        ///A test for Execute
        ///</summary>
        [TestMethod()]
        public void CanSyncLocalInstanceToFromRemoteInstance()
        {
            ScriptRunner scriptRunner = new ScriptRunner(Language.CSharp); // TODO: Initialize to an appropriate value

            string fullTypeName = "ObjectBakery.Tests.TestDoubles.Asset";
            string filePath = @".\Asset.cs";

            object builtAsset = scriptRunner.Build(fullTypeName, filePath);
            Asset sourceAsset = new Asset() { ID = 5, Name = "ABC" };

            

            DataSyncHelper syncHelper = new DataSyncHelper();
            syncHelper.Sync(sourceAsset, builtAsset);

         
            Assert.IsTrue( Convert.ToInt32(scriptRunner.GetProperty("ID", builtAsset)) == 5 &&
                          scriptRunner.GetProperty("Name", builtAsset).ToString() == "ABC");

        }


        /// <summary>
        ///A test for Execute
        ///</summary>
        [TestMethod()]
        public void CanGetSetProperty()
        {
            ScriptRunner scriptRunner = new ScriptRunner(Language.CSharp); // TODO: Initialize to an appropriate value

            string fullTypeName = "ObjectBakery.Tests.TestDoubles.Asset";
            string filePath = @".\Asset.cs";

            object builtAsset = scriptRunner.Build(fullTypeName, filePath);
            Asset targetAsset = new Asset();

            scriptRunner.SetProperty("Name", "Blah", builtAsset);

            object retVal = scriptRunner.GetProperty("Name", builtAsset);

            Assert.IsTrue(retVal.ToString().Equals("Blah"));


        }

        //[TestMethod()]
        //public void TestRunMultiply()
        //{
        //    var target = new ScriptRunner(Language.CSharp);
        //    target.Load("ObjectBakery.Script", @".\ScriptRunner.cs");
        //    int result = target.RunMultiply(5, 10);
        //    Assert.IsTrue(result.Equals(50));
        //}
    }
}
